package lily.phs.pregnance;

import lily.phs.object.HereTipa;
import lily.phs.object.PostnatalVisit;
import lily.phs.urh.R;
import lily.ui.control.MyPopupWindow;


import android.content.Context;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * 产后访视记录表：界面布局和数据处理
 * @author heckassitant
 *
 */
public class PostnatalVisitView extends LinearLayout implements OnClickListener, OnFocusChangeListener{

	private MyPopupWindow mPopupWindow;
	private Context mContext;
	private int profileid=0;
	
	private EditText et_name,et_id,et_date,et_body_temprature,et_ordinary_healthstatus,et_ordinary_mentalstatus,
	                 et_bloodpressure_high,et_bloodpressure_low,et_breast_abnormal,et_lochia_abnormal,et_uterus_abnormal,
	                 et_wound_abnormal,et_other_abnormal,et_abnormal,et_guide_other,et_transfer_reason,et_transfer_hospital,
	                 et_next_visit_date,et_doctor;
	private RadioGroup rgHasBreastAbnormal,rgHasLochiaAbnormal,rgHasUterusAbnormal,rgHasWoundAbnormal,rgIsAbnormal,rgNeedTransfer;
	private RadioButton rb_has_breast_abnormal_no,rb_has_breast_abnormal_yes,rb_has_lochia_abnormal_no,rb_has_lochia_abnormal_yes,
	                    rb_has_uterus_abnormal_no,rb_has_uterus_abnormal_yes,rb_has_wound_abnormal_no,rb_has_wound_abnormal_yes,
	                    rb_is_abnormal_no,rb_is_abnormal_yes,rb_need_transfer_no,rb_need_transfer_yes;
	
	private CheckBox cb_guide_1,cb_guide_2,cb_guide_3,cb_guide_4,cb_guide_5,cb_guide_6;
	
	private HereTipa ht;
	private PostnatalVisit pv;
	private View child;
	
	public PostnatalVisitView(Context context,int profileId) {
		super(context);
		// TODO Auto-generated constructor stub
		this.profileid = profileId;
		this.mContext = context;
		
		final LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		child = inflater.inflate(R.layout.tablepreg3, null);
	}

	/**
	 * 与xml文件关联
	 */
	public void findViewsById() {
		// TODO Auto-generated method stub
		et_name = (EditText)child.findViewById(R.id.post_name);
		et_id = (EditText)child.findViewById(R.id.post_id);
		et_date = (EditText)child.findViewById(R.id.post_date);
		et_date.setInputType(InputType.TYPE_NULL);
		et_date.setOnClickListener(this);
		et_date.setOnFocusChangeListener(this);
		et_body_temprature = (EditText)child.findViewById(R.id.post_body_temprature);
		et_ordinary_healthstatus = (EditText)child.findViewById(R.id.post_ordinary_healthstatus);
		et_ordinary_mentalstatus = (EditText)child.findViewById(R.id.post_ordinary_mentalstatus);
        et_bloodpressure_high = (EditText)child.findViewById(R.id.post_bloodpressure_high);
        et_bloodpressure_low = (EditText)child.findViewById(R.id.post_bloodpressure_low);
        et_breast_abnormal = (EditText)child.findViewById(R.id.post_breast_abnormal);
        et_breast_abnormal.setOnFocusChangeListener(this);
        et_breast_abnormal.setOnClickListener(this);
        et_lochia_abnormal = (EditText)child.findViewById(R.id.post_lochia_abnormal);
        et_lochia_abnormal.setOnFocusChangeListener(this);
        et_lochia_abnormal.setOnClickListener(this);
        et_uterus_abnormal = (EditText)child.findViewById(R.id.post_uterus_abnormal);
        et_uterus_abnormal.setOnFocusChangeListener(this);
        et_uterus_abnormal.setOnClickListener(this);
        et_wound_abnormal = (EditText)child.findViewById(R.id.post_wound_abnormal);
        et_wound_abnormal.setOnFocusChangeListener(this);
        et_wound_abnormal.setOnClickListener(this);
        et_other_abnormal = (EditText)child.findViewById(R.id.post_other_abnormal);
        et_abnormal = (EditText)child.findViewById(R.id.post_abnormal);
        et_abnormal.setOnFocusChangeListener(this);
        et_abnormal.setOnClickListener(this);
        et_guide_other = (EditText)child.findViewById(R.id.post_guide_other);
        et_guide_other.setOnFocusChangeListener(this);
        et_guide_other.setOnClickListener(this);
        et_transfer_reason = (EditText)child.findViewById(R.id.post_transfer_reason);
        et_transfer_reason.setOnFocusChangeListener(this);
        et_transfer_reason.setOnClickListener(this);
        et_transfer_hospital = (EditText)child.findViewById(R.id.post_transfer_hospital);
        et_transfer_hospital.setOnFocusChangeListener(this);
        et_transfer_hospital.setOnClickListener(this);
        et_next_visit_date = (EditText)child.findViewById(R.id.post_next_visit_date);
        et_next_visit_date.setInputType(InputType.TYPE_NULL);
        et_next_visit_date.setOnClickListener(this);
        et_next_visit_date.setOnFocusChangeListener(this);
        et_doctor = (EditText)child.findViewById(R.id.post_doctor);
		rb_has_breast_abnormal_no = (RadioButton)child.findViewById(R.id.post_has_breast_abnormal_no);
		rb_has_breast_abnormal_yes = (RadioButton)child.findViewById(R.id.post_has_breast_abnormal_yes);
		rb_has_lochia_abnormal_no = (RadioButton)child.findViewById(R.id.post_has_lochia_abnormal_no);
		rb_has_lochia_abnormal_yes = (RadioButton)child.findViewById(R.id.post_has_lochia_abnormal_yes);
        rb_has_uterus_abnormal_no = (RadioButton)child.findViewById(R.id.post_has_uterus_abnormal_no);
        rb_has_uterus_abnormal_yes = (RadioButton)child.findViewById(R.id.post_has_uterus_abnormal_yes);
        rb_has_wound_abnormal_no = (RadioButton)child.findViewById(R.id.post_has_wound_abnormal_no);
        rb_has_wound_abnormal_yes = (RadioButton)child.findViewById(R.id.post_has_wound_abnormal_yes);
        rb_is_abnormal_no = (RadioButton)child.findViewById(R.id.post_is_abnormal_no);
        rb_is_abnormal_yes = (RadioButton)child.findViewById(R.id.post_is_abnormal_yes);
        rb_need_transfer_no = (RadioButton)child.findViewById(R.id.post_need_transfer_no);
        rb_need_transfer_yes = (RadioButton)child.findViewById(R.id.post_need_transfer_yes);
		cb_guide_1 = (CheckBox)child.findViewById(R.id.post_guide_1);
		cb_guide_2 = (CheckBox)child.findViewById(R.id.post_guide_2);
		cb_guide_3 = (CheckBox)child.findViewById(R.id.post_guide_3);
		cb_guide_4 = (CheckBox)child.findViewById(R.id.post_guide_4);
		cb_guide_5 = (CheckBox)child.findViewById(R.id.post_guide_5);
		cb_guide_6 = (CheckBox)child.findViewById(R.id.post_guide_6);
		rgHasBreastAbnormal = (RadioGroup)child.findViewById(R.id.RadioGroup04);
		rgHasLochiaAbnormal = (RadioGroup)child.findViewById(R.id.RadioGroup07);
		rgHasUterusAbnormal = (RadioGroup)child.findViewById(R.id.RadioGroup06);
		rgHasWoundAbnormal = (RadioGroup)child.findViewById(R.id.RadioGroup05);
		rgIsAbnormal = (RadioGroup)child.findViewById(R.id.RadioGroup12);
		rgNeedTransfer = (RadioGroup)child.findViewById(R.id.RadioGroup13);
	}
	/**
	 * 检出数据并初始化views
	 */
	public void initViews() {
		// TODO Auto-generated method stub
		initialState();
		
		String temp = "";
		int itemp=-1;
		float ftemp=-1f;
		
		temp = ht.getHeti_name();
		et_name.setText(temp);
		et_name.setEnabled(false);
		temp = ht.getHeti_here_id();
		et_id.setText(temp);
		et_id.setEnabled(false);
		
		temp = pv.getPovi_date();
		et_date.setText(temp);
		ftemp = pv.getPovi_body_temprature();
		if(ftemp!=-1f){
			et_body_temprature.setText(String.valueOf(ftemp));
		}
		temp = pv.getPovi_ordinary_healthstatus();
		et_ordinary_healthstatus.setText(temp);
		temp = pv.getPovi_ordinary_mentalstatus();
	    et_ordinary_mentalstatus.setText(temp);
	    
	    itemp = pv.getPovi_bloodpressure_high();
	    if(itemp!=-1){
	    	et_bloodpressure_high.setText(String.valueOf(itemp));
	    }
	    itemp = pv.getPovi_bloodpressure_low();
	    if(itemp!=-1){
	    	et_bloodpressure_low.setText(String.valueOf(itemp));
	    }
		
		itemp = pv.getPovi_has_breast_abnormal();
		switch(itemp){
		case 1:
			rgHasBreastAbnormal.check(0);
			rb_has_breast_abnormal_no.setChecked(true);
			break;
		case 2:{
			rgHasBreastAbnormal.check(1);
			rb_has_breast_abnormal_yes.setChecked(true);
			temp = pv.getPovi_breast_abnormal();
			et_breast_abnormal.setText(temp);
		}
		}
		itemp = pv.getPovi_has_lochia_abnormal();
		switch(itemp){
		case 1:
			rgHasLochiaAbnormal.check(0);
			rb_has_lochia_abnormal_no.setChecked(true);
			break;
		case 2:{
			rgHasLochiaAbnormal.check(1);
			rb_has_lochia_abnormal_yes.setChecked(true);
			temp = pv.getPovi_lochia_abnormal();
			et_lochia_abnormal.setText(temp);
		}
		}
		itemp = pv.getPovi_has_uterus_abnormal();
		switch(itemp){
		case 1:
			rgHasUterusAbnormal.check(0);
			rb_has_uterus_abnormal_no.setChecked(true);
			break;
		case 2:{
			rgHasUterusAbnormal.check(1);
			rb_has_uterus_abnormal_yes.setChecked(true);
			temp = pv.getPovi_uterus_abnormal();
			et_uterus_abnormal.setText(temp);
		}
		}
		itemp = pv.getPovi_has_wound_abnormal();
		switch(itemp){
		case 1:
			rgHasWoundAbnormal.check(0);
			rb_has_wound_abnormal_no.setChecked(true);
			break;
		case 2:{
			rgHasWoundAbnormal.check(1);
			rb_has_wound_abnormal_yes.setChecked(true);
			temp = pv.getPovi_wound_abnormal();
			et_wound_abnormal.setText(temp);
		}
		}
		temp = pv.getPovi_other_abnormal();
		et_other_abnormal.setText(temp);
		itemp = pv.getPovi_is_abnormal();
		switch(itemp){
		case 1:
			rgIsAbnormal.check(0);
			rb_is_abnormal_no.setChecked(true);
			break;
		case 2:{
			rgIsAbnormal.check(1);
			rb_is_abnormal_yes.setChecked(true);
			temp = pv.getPovi_abnormal();
			et_abnormal.setText(temp);
		}
		}
		
		temp = pv.getPovi_guide();
		if(temp.length()>0){
			String[] ss = temp.split(",");
			for(int i=0;i<ss.length;i++){
				int k = Integer.parseInt(ss[i]);
				switch(k){
				case 1:
					cb_guide_1.setChecked(true);
					break;
				case 2:
					cb_guide_2.setChecked(true);
					break;
				case 3:
					cb_guide_3.setChecked(true);
					break;
				case 4:
					cb_guide_4.setChecked(true);
					break;
				case 5:
					cb_guide_5.setChecked(true);
					break;
				case 6:{
					cb_guide_6.setChecked(true);
					temp = pv.getPovi_other_guide();
					et_guide_other.setText(temp);
					break;
				}
				}
			}
		}
		
		itemp = pv.getPovi_need_transfer();
		switch(itemp){
		case 1:
			rgNeedTransfer.check(0);
			rb_need_transfer_no.setChecked(true);
			break;
		case 2:{
			rgNeedTransfer.check(1);
			rb_need_transfer_yes.setChecked(true);
			temp = pv.getPovi_transfer_reason();
			et_transfer_reason.setText(temp);
			temp = pv.getPovi_transfer_hospital();
			et_transfer_hospital.setText(temp);
		}
		}
		temp = pv.getPovi_next_visit_date();
		et_next_visit_date.setText(temp);
		temp = pv.getPovi_doctor();
		et_doctor.setText(temp);
				
	}
	
	/**
	 * 初始时：横线为空，所有选择项未选中
	 */
	private void initialState() {
		// TODO Auto-generated method stub
		et_name.setText("");
		et_id.setText("");
		et_date.setText("");
		et_body_temprature.setText("");
		et_ordinary_healthstatus.setText("");
		et_ordinary_mentalstatus.setText("");
        et_bloodpressure_high.setText("");
        et_bloodpressure_low.setText("");
        et_breast_abnormal.setText("");
        et_lochia_abnormal.setText("");
        et_uterus_abnormal.setText("");
        et_wound_abnormal.setText("");
        et_other_abnormal.setText("");
        et_abnormal.setText("");
        et_guide_other.setText("");
        et_transfer_reason.setText("");
        et_transfer_hospital.setText("");
        et_next_visit_date.setText("");
        et_doctor.setText("");
		rb_has_breast_abnormal_no.setChecked(false);
		rb_has_breast_abnormal_yes.setChecked(false);
		rb_has_lochia_abnormal_no.setChecked(false);
		rb_has_lochia_abnormal_yes.setChecked(false);
        rb_has_uterus_abnormal_no.setChecked(false);
        rb_has_uterus_abnormal_yes.setChecked(false);
        rb_has_wound_abnormal_no.setChecked(false);
        rb_has_wound_abnormal_yes.setChecked(false);
        rb_is_abnormal_no.setChecked(false);
        rb_is_abnormal_yes.setChecked(false);
        rb_need_transfer_no.setChecked(false);
        rb_need_transfer_yes.setChecked(false);
        cb_guide_1.setChecked(false);
        cb_guide_2.setChecked(false);
        cb_guide_3.setChecked(false);
        cb_guide_4.setChecked(false);
        cb_guide_5.setChecked(false);
        cb_guide_6.setChecked(false);
		
	}

	/**
	 * 添加view到父view中
	 */
	public void setMyView(){
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		this.addView(child,lp);
	}
	
	/**
	 * 将整个界面作为一个view对象
	 * @return 整个界面view
	 */
	public View getMyView(){
		return this.child;
	}
	
	/**
	 * 检出数据
	 */
	public void fetchData(){
		
		ht = new HereTipa(mContext);
		ht.selectNameAndCode(this.profileid);
		
		pv = new PostnatalVisit(mContext);
		pv.selectPostnatalVisit(this.profileid);
		
	}

	/**
	 * 保存数据
	 */
	public void saveViews() {
		// TODO Auto-generated method stub
		String temp = "";
		StringBuffer sb=null;
		
		PostnatalVisit pv = new PostnatalVisit(mContext);
		pv.setPovi_id(this.profileid);
		pv.selectPostnatalVisit(profileid);
		
		temp = et_date.getText().toString();
		pv.setPovi_date(temp);
		temp = et_body_temprature.getText().toString();
		if(temp.length()>0){
			pv.setPovi_body_temprature(Float.parseFloat(temp));
		}
		temp = et_ordinary_healthstatus.getText().toString();
		pv.setPovi_ordinary_healthstatus(temp);
		temp = et_ordinary_mentalstatus.getText().toString();
		pv.setPovi_ordinary_mentalstatus(temp);
		temp = et_bloodpressure_high.getText().toString();
		if(temp.length()>0){
			pv.setPovi_bloodpressure_high(Integer.parseInt(temp));
		}
		temp = et_bloodpressure_low.getText().toString();
		if(temp.length()>0){
			pv.setPovi_bloodpressure_low(Integer.parseInt(temp));
		}
		if(rb_has_breast_abnormal_no.isChecked()==true){
			pv.setPovi_has_breast_abnormal(1);
		}else if(rb_has_breast_abnormal_yes.isChecked()==true){
			pv.setPovi_has_breast_abnormal(2);
			temp = et_breast_abnormal.getText().toString();
			pv.setPovi_breast_abnormal(temp);
		}
		if(rb_has_lochia_abnormal_no.isChecked()==true){
			pv.setPovi_has_lochia_abnormal(1);
		}else if(rb_has_lochia_abnormal_yes.isChecked()==true){
			pv.setPovi_has_lochia_abnormal(2);
			temp = et_lochia_abnormal.getText().toString();
			pv.setPovi_lochia_abnormal(temp);
		}
		if(rb_has_uterus_abnormal_no.isChecked()==true){
			pv.setPovi_has_uterus_abnormal(1);
		}else if(rb_has_uterus_abnormal_yes.isChecked()==true){
			pv.setPovi_has_uterus_abnormal(2);
			temp = et_uterus_abnormal.getText().toString();
			pv.setPovi_uterus_abnormal(temp);
		}
		if(rb_has_wound_abnormal_no.isChecked()==true){
			pv.setPovi_has_wound_abnormal(1);
		}else if(rb_has_wound_abnormal_yes.isChecked()==true){
			pv.setPovi_has_wound_abnormal(2);
			temp = et_wound_abnormal.getText().toString();
			pv.setPovi_wound_abnormal(temp);
		}
		temp = et_other_abnormal.getText().toString();
		pv.setPovi_other_abnormal(temp);
		if(rb_is_abnormal_no.isChecked()==true){
			pv.setPovi_is_abnormal(1);
		}else if(rb_is_abnormal_yes.isChecked()==true){
			pv.setPovi_is_abnormal(2);
			temp = et_abnormal.getText().toString();
			pv.setPovi_abnormal(temp);
		}
		
		sb = new StringBuffer();
		if(cb_guide_1.isChecked()==true){
			sb.append("1,");
		}
		if(cb_guide_2.isChecked()==true){
			sb.append("2,");
		}
		if(cb_guide_3.isChecked()==true){
			sb.append("3,");
		}
		if(cb_guide_4.isChecked()==true){
			sb.append("4,");
		}
		if(cb_guide_5.isChecked()==true){
			sb.append("5,");
		}
		if(cb_guide_6.isChecked()==true){
			sb.append("6,");
			temp = et_guide_other.getText().toString();
			pv.setPovi_other_guide(temp);
		}
		pv.setPovi_guide(sb.toString());
		
		if(rb_need_transfer_no.isChecked()==true){
			pv.setPovi_need_transfer(1);
		}else if(rb_need_transfer_yes.isChecked()==true){
			pv.setPovi_need_transfer(2);
			temp = et_transfer_reason.getText().toString();
			pv.setPovi_transfer_reason(temp);
			temp = et_transfer_hospital.getText().toString();
			pv.setPovi_transfer_hospital(temp);
		}
		
		temp = et_next_visit_date.getText().toString();
		pv.setPovi_next_visit_date(temp);
		temp = et_doctor.getText().toString();
		pv.setPovi_doctor(temp);
		
		pv.updatePostnatalVisit(this.profileid);
		
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==et_date){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(findViewById(R.id.post_date), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_date.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_next_visit_date){
			mPopupWindow = new MyPopupWindow(mContext);			
			mPopupWindow.showAtLocation(findViewById(R.id.post_next_visit_date), Gravity.CENTER, 0, 0);

			Button btn=mPopupWindow.getButton();
			btn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					et_next_visit_date.setText(mPopupWindow.getDate());
					mPopupWindow.dismiss();
				}
				
			});
		}
		else if(v==et_breast_abnormal){
			rb_has_breast_abnormal_no.setChecked(false);
			rb_has_breast_abnormal_yes.setChecked(true);
		}
		else if(v==et_lochia_abnormal){
			rb_has_lochia_abnormal_no.setChecked(false);
			rb_has_lochia_abnormal_yes.setChecked(true);
		}
		else if(v==et_uterus_abnormal){
			rb_has_uterus_abnormal_no.setChecked(false);
			rb_has_uterus_abnormal_yes.setChecked(true);
		}
		else if(v==et_wound_abnormal){
			rb_has_wound_abnormal_no.setChecked(false);
			rb_has_wound_abnormal_yes.setChecked(true);
		}
		else if(v==et_abnormal){
			rb_is_abnormal_no.setChecked(false);
			rb_is_abnormal_yes.setChecked(true);
		}
		else if(v==et_guide_other){
			cb_guide_6.setChecked(true);
		}
		else if((v==et_transfer_reason||v==et_transfer_hospital)){
			rb_need_transfer_no.setChecked(false);
			rb_need_transfer_yes.setChecked(true);
			
		}
		
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		if(hasFocus){
			if(v==et_breast_abnormal){
				rb_has_breast_abnormal_no.setChecked(false);
				rb_has_breast_abnormal_yes.setChecked(true);
			}
			else if(v==et_lochia_abnormal){
				rb_has_lochia_abnormal_no.setChecked(false);
				rb_has_lochia_abnormal_yes.setChecked(true);
			}
			else if(v==et_uterus_abnormal){
				rb_has_uterus_abnormal_no.setChecked(false);
				rb_has_uterus_abnormal_yes.setChecked(true);
			}
			else if(v==et_wound_abnormal){
				rb_has_wound_abnormal_no.setChecked(false);
				rb_has_wound_abnormal_yes.setChecked(true);
			}
			else if(v==et_abnormal){
				rb_is_abnormal_no.setChecked(false);
				rb_is_abnormal_yes.setChecked(true);
			}
			else if(v==et_guide_other){
				cb_guide_6.setChecked(true);
			}
			else if((v==et_transfer_reason||v==et_transfer_hospital)){
				rb_need_transfer_no.setChecked(false);
				rb_need_transfer_yes.setChecked(true);
			}
			else if(v==et_date){
				mPopupWindow = new MyPopupWindow(getContext());			
				mPopupWindow.showAtLocation(findViewById(R.id.post_date  ), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_date.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
			}
			else if(v==et_next_visit_date){
				mPopupWindow = new MyPopupWindow(getContext());			
				mPopupWindow.showAtLocation(findViewById(R.id.post_next_visit_date  ), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_next_visit_date.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
			}
		}
			
	}
}
